//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.IO.NetIO;

import com.goldhuman.Common.Conf;
import com.goldhuman.Common.Octets;
import com.goldhuman.Common.Security.Security;

import java.net.SocketAddress;

public abstract class NetSession implements Cloneable {
    private static final int DEFAULTIOBUF = 8192;
    protected Octets ibuffer = new Octets(8192);
    protected Octets obuffer = new Octets(8192);
    protected Octets isecbuf = new Octets(8192);
    Security isec = Security.Create("NULLSECURITY");
    Security osec = Security.Create("NULLSECURITY");
    protected boolean closing = false;
    protected SocketAddress peeraddress = null;

    public synchronized void setPeerAddress(SocketAddress peer) {
        this.peeraddress = peer;
    }

    public synchronized SocketAddress getPeerAddress() {
        return this.peeraddress;
    }

    public NetSession() {
    }

    protected boolean Output(Octets data) {
        if (data.size() + this.obuffer.size() > this.obuffer.capacity()) {
            return false;
        } else {
            this.osec.Update(data);
            this.obuffer.insert(this.obuffer.size(), data);
            return true;
        }
    }

    protected Octets Input() {
        this.isec.Update(this.ibuffer);
        this.isecbuf.insert(this.isecbuf.size(), this.ibuffer);
        this.ibuffer.clear();
        return this.isecbuf;
    }

    public void SetISecurity(String type, Octets key) {
        synchronized(this) {
            this.isec = Security.Create(type);
            this.isec.SetParameter(key);
        }
    }

    public void SetOSecurity(String type, Octets key) {
        synchronized(this) {
            this.osec = Security.Create(type);
            this.osec.SetParameter(key);
        }
    }

    public void LoadConfig() {
        Conf conf = Conf.GetInstance();
        String section = this.Identification();

        try {
            this.ibuffer.reserve(Integer.parseInt(conf.find(section, "ibuffermax")));
        } catch (Exception var7) {
        }

        try {
            this.obuffer.reserve(Integer.parseInt(conf.find(section, "obuffermax")));
        } catch (Exception var6) {
        }

        try {
            this.SetISecurity(conf.find(section, "isec").trim(), new Octets(conf.find(section, "iseckey").getBytes()));
        } catch (Exception var5) {
        }

        try {
            this.SetOSecurity(conf.find(section, "osec").trim(), new Octets(conf.find(section, "oseckey").getBytes()));
        } catch (Exception var4) {
        }

    }

    protected void Close() {
        this.closing = true;
    }

    protected abstract void OnRecv();

    protected abstract void OnSend();

    protected abstract void OnOpen();

    protected abstract void OnClose();

    public void OnAbort() {
    }

    public abstract String Identification();

    public SocketAddress OnCheckAddress(SocketAddress sa) {
        return sa;
    }

    public Object clone() {
        try {
            NetSession s = (NetSession)super.clone();
            s.ibuffer = new Octets(this.ibuffer.capacity());
            s.obuffer = new Octets(this.obuffer.capacity());
            s.isecbuf = new Octets(this.isecbuf.capacity());
            s.isec = (Security)this.isec.clone();
            s.osec = (Security)this.osec.clone();
            return s;
        } catch (Exception var2) {
            return null;
        }
    }
}
